<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 用户控制器
 */

namespace app\common\model;

use think\Model;

class Notify extends Base
{


    // 根据菜单权限发送给用户
    function sendToUserByMenu($menu, $tpl, $tpl_data)
    {
        // 根据菜单朝招
        $where_menu[] = ['url', 'eq', $menu];
        $menu_id_list = model('Menu')->where($where_menu)->column('id');

        if (empty($menu_id_list)) {
            return;
        }

        // 根据菜单ID获取角色ID
        $where_role[] = ['menu_id', 'in', $menu_id_list];
        $role_id_list = model('RoleMenu')->where($where_role)->column('role_id');

        if (empty($role_id_list)) {
            return;
        }
        // 根据角色获取用户ID
        $where_user[] = ['role_id', 'in', $role_id_list];
        $user_id_list = model('RoleUser')->where($where_user)->column('user_id');

        if (empty($user_id_list)) {
            return;
        } else {
            foreach ($user_id_list as $user_id) {
                foreach ($tpl_data as $key => $val) {
                    $tpl['content'] = str_replace('{' . $key . '}', $val, $tpl['content']);
                }
                $this->send($user_id, $tpl, 'user');
            }
        }
    }


    // 根据菜单权限发送给用户
    function sendToSupplierByMenu($supplier_id, $menu, $tpl, $tpl_data)
    {
        $supplier = Supplier::find($supplier_id);

        // 根据菜单照菜单ID
        $where_menu[] = ['url', 'eq', $menu];
        $menu_id_list = model('SupplierMenu')->where($where_menu)->column('id');

        if (empty($menu_id_list)) {
            return;
        }

        // 根据菜单ID获取角色ID
        $where_role[] = ['menu_id', 'in', $menu_id_list];

        $role_id_list = model('SupplierRoleMenu')->where($where_role)->column('role_id');

        if (empty($role_id_list)) {
            return;
        }

        // 根据角色获取用户ID
        $where_user[] = ['role_id', 'in', $role_id_list];
        $where_user[] = ['supplier_id', 'eq', $supplier_id];
        $user_id_list = model('SupplierRoleUser')->where($where_user)->column('user_id');

        $where_super[] = ['supplier_id', 'eq', $supplier_id];
        $where_super[] = ['is_super', 'eq', 1];
        $supplier_user = model('SupplierUser')->where($where_super)->column('id');

        $user_id_list = array_merge($user_id_list, $supplier_user);

        if (empty($user_id_list)) {
            return;
        } else {
            foreach ($user_id_list as $user_id) {
                foreach ($tpl_data as $key => $val) {
                    $tpl['content'] = str_replace('{' . $key . '}', $val, $tpl['content']);
                }
                $this->send($user_id, $tpl, 'supplier');
            }
        }
    }

    function sendToPurchaser($receiver_id, $tpl, $tpl_data)
    {
        foreach ($tpl_data as $key => $val) {
            $tpl['content'] = str_replace('{' . $key . '}', $val, $tpl['content']);
        }
        return $this->send($receiver_id, $tpl, 'purchaser');
    }

    // 发送通知
    function send($receiver_id, $data, $type)
    {
        $data['receiver_id'] = $receiver_id;
        $data['type']        = $type;
        $ret                 = $this->create($data);

        return $ret;
    }

// 标记已读
    function markIsRead($id)
    {
        if (is_array($id)) {
            $where[] = ['id', 'in', $id];
            $ret     = $this->where($where)->setField('is_read', 1);
        }

        if (is_numeric($id)) {
            $notify          = $this->find($id);
            $notify->is_read = 1;
            $ret             = $notify->save();
        }

        return $ret;
    }

// 标记已读根据 接收人ID
    function markIsReadByReceiverId($receiver_id, $type)
    {
        $where[] = ['receiver_id', 'eq', $receiver_id];
        $where[] = ['type', 'eq', $type];

        $ret = $this->where($where)->setField('is_read', 1);

        return $ret;
    }

// 未读统计
    function getTodoCount($receiver_id, $type)
    {
        $where[] = ['receiver_id', 'eq', $receiver_id];
        $where[] = ['type', 'eq', $type];
        $where[] = ['is_read', 'eq', 0];

        $count = $this->where($where)->count();

        return $count;
    }

// 删除
    function del($id)
    {
        if (is_array($id)) {
            $where[] = ['id', 'in', $id];
            $ret     = $this->where($where)->setField('is_del', 1);
        }

        if (is_numeric($id)) {
            $notify         = $this->find($id);
            $notify->is_del = 1;
            $ret            = $notify->save();
        }

        return $ret;
    }

// 删除全部
    function delByReceiverId($receiver_id, $type)
    {
        $where[] = ['receiver_id', 'eq', $receiver_id];
        $where[] = ['type', 'eq', $type];
        $ret     = $this->where($where)->setField('is_del', 1);

        return $ret;
    }
}
